package com.tools.cleanmaster.ui.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Interpolator;
import android.widget.Button;

import com.tools.cleanmaster.R;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * CreatedXXX on 17/8/17.
 */

public class CommonWaveButton extends Button implements View.OnClickListener {
    private Interpolator mInterpolator = new AccelerateInterpolator();
    private float mInitialRadius = 0;   // 初始波纹半径
    private float mMaxRadiusRate = 0.95f;
    private float mMaxRadius = 0;   // 最大波纹半径
    private long mDuration = 2000; // 一个波纹从创建到消失的持续时间
    private int mSpeed = 1000;   // 波纹的创建速度
    public static final int DEFAULT_CIRCLE_COLOR = 0xffff0000;

    private List<Circle> mCircleList = new ArrayList<Circle>();
    private boolean mIsRunning;
    private Paint mPaint;
    private long mLastCreateTime;

    private OnClickListener mOnClickListener;

    private Runnable mCreateCircle = new Runnable() {
        @Override
        public void run() {
            if (mIsRunning) {
                newCircle();
                postDelayed(mCreateCircle, mSpeed);
            }
        }
    };

    public CommonWaveButton(Context context) {
        super(context, null);
    }

    public CommonWaveButton(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
        this.setClickable(true);

        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setStyle(Paint.Style.FILL);
        TypedArray typedArray = context.obtainStyledAttributes(attributeSet, R.styleable.CommonWaveButton);
        mMaxRadius = typedArray.getDimension(R.styleable.CommonWaveButton_max_radius, 0);
        mInitialRadius = typedArray.getDimension(R.styleable.CommonWaveButton_initial_radius, 0);
        mDuration = typedArray.getInt(R.styleable.CommonWaveButton_wave_duration, 2000);
        mSpeed = typedArray.getInt(R.styleable.CommonWaveButton_wave_speed, 2000);
        mPaint.setColor(typedArray.getColor(R.styleable.CommonWaveButton_wave_color, DEFAULT_CIRCLE_COLOR));
        typedArray.recycle();

        setOnClickListener(this);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        onWaveDraw(canvas);
        super.onDraw(canvas);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        mMaxRadius = mMaxRadius > 1 ? mMaxRadiusRate : Math.max(getWidth(), getHeight()) * mMaxRadiusRate / 2.0f;
        mInitialRadius = mInitialRadius > 1 ? mInitialRadius : 0;
    }

    public void start() {
        if (!mIsRunning) {
            mIsRunning = true;
            mCreateCircle.run();
        }
    }

    public void stop() {
        mIsRunning = false;
        mCircleList.clear();
    }

    public void setInterPolator(Interpolator interPolator) {
        this.mInterpolator = interPolator;
    }

    private void onWaveDraw(Canvas canvas) {
        Iterator<Circle> iterator = mCircleList.iterator();
        while (iterator.hasNext()) {
            Circle circle = iterator.next();
            if (System.currentTimeMillis() - circle.mCreateTime < mDuration) {
                mPaint.setAlpha(circle.getAlpha());
                canvas.drawCircle(getWidth() / 2, getHeight() / 2, circle.getCurrentRadius(), mPaint);
            } else {
                iterator.remove();
            }
        }
        if (mCircleList.size() > 0) {
            postInvalidateDelayed(10);
        }
    }

    private void newCircle() {
        long currentTime = System.currentTimeMillis();
        if (currentTime - mLastCreateTime < mSpeed) {
            return;
        }
        Circle circle = new Circle();
        mCircleList.add(circle);
        invalidate();
        mLastCreateTime = currentTime;
    }

    public void addOnClickListener(@Nullable OnClickListener l) {
        mOnClickListener = l;
    }

    @Override
    public void onClick(View v) {
        stop();
        if (mOnClickListener != null) {
            mOnClickListener.onClick(v);
        }
    }

    public void onDestroy() {
        stop();
        mOnClickListener = null;
    }

    private class Circle {
        private long mCreateTime;

        public Circle() {
            this.mCreateTime = System.currentTimeMillis();
        }

        public int getAlpha() {
            float percent = (System.currentTimeMillis() - mCreateTime) * 0.3f / mDuration;
            if (percent < 0.1f) {
                return (int) (mInterpolator.getInterpolation(percent) * 255);
            }
            return (int) ((0.2f - mInterpolator.getInterpolation(percent)) * 255);
        }

        public float getCurrentRadius() {
            float percent = (System.currentTimeMillis() - mCreateTime) * 1.0f / mDuration;
            return mInitialRadius + mInterpolator.getInterpolation(percent) * (mMaxRadius - mInitialRadius);
        }
    }

}
